package com.cuijq.ms.Iteratordesign.v2;


/**
 * @author by CuiJQ
 * @Classname ArrayList
 * @Description ArrayList Description
 * @Date 2021/5/29 16:50
 */
public class ArrayList<E> implements List<E> {

  private static final int DEFAULT_CAPACITY = 10;
  private int actualSize;
  //不包含标记删除元素
  private int totalSize;
  //包含标记删除元素
  private Object[] elements;
  private long[] addTimestamps;
  private long[] delTimestamps;

  public ArrayList() {
    this.elements = new Object[DEFAULT_CAPACITY];
    this.addTimestamps = new long[DEFAULT_CAPACITY];
    this.delTimestamps = new long[DEFAULT_CAPACITY];
    this.totalSize = 0;
    this.actualSize = 0;
  }

  public void add(E obj) {
    elements[totalSize] = obj;
    addTimestamps[totalSize] = System.currentTimeMillis();
    delTimestamps[totalSize] = Long.MAX_VALUE;
    totalSize++;
    actualSize++;
  }

  public void remove(E obj) {
    for (int i = 0; i < totalSize; ++i) {
      if (elements[i].equals(obj)) {
        delTimestamps[i] = System.currentTimeMillis();
        actualSize--;
      }
    }
  }

  public int actualSize() {
    return this.actualSize;
  }

  public int totalSize() {
    return this.totalSize;
  }
  public int size() {
    return this.totalSize;
  }
  @Override
  public Iterator iterator() { return new ArrayIterator(this); }

  public E get(int cursor) {
    return (E) elements[cursor];
  }

  public long getAddTimestamp(int cursorInAll) {
    return 0;
  }

  public long getDelTimestamp(int cursorInAll) {
    return 0;
  }
}
